package com.ygou.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ygou.pojo.Admininfo;
import com.ygou.service.AdminInfoService;
import com.ygou.utils.AppendContentToFile;
import com.ygou.utils.MD5Util;
import com.ygou.utils.Tools;

@Controller
@RequestMapping(value = "/admin")
public class AdmininfoController {
	@Autowired
	AdminInfoService adminInfo;
	@Autowired
	HttpServletResponse response;

	@RequestMapping(value = "/show")
	public String showDetail(String name, Integer currPageNo, Integer typeId, ModelMap model) {
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("selName", name);
		m.put("currPage", currPageNo);
		m.put("typeId", typeId);
		m = adminInfo.showPage(m);
		PageInfo<Admininfo> pages = (PageInfo<Admininfo>) m.get("pages");
		model.put("pageInfo", pages);
		return "admin/admin_info";
	}

	@RequestMapping(value = "/insert",produces="application/json;charset=UTF-8")
	public @ResponseBody boolean insertAdmin(HttpSession session,Admininfo admin, String loginpwd, HttpServletRequest request) throws IOException {
		// 加密密码
		admin.setLoginpwd(MD5Util.getMd5(loginpwd));
		// ip地址
		admin.setLastip(Tools.getLocalIp(request));
		/*******写入日志******/
		Admininfo admins= (Admininfo)session.getAttribute("admin");
		int id=100;
		if(admins!=null){
			id=admins.getAdminid();
		}
		AppendContentToFile.fileLogPrint(id+"", "添加了管理员");
		/*******END写入日志******/
		return adminInfo.insert(admin);
	}

	@RequestMapping(value = "/selectByPrimaryKey")
	public String selectByPrimaryKey(Integer adminid,String type, ModelMap model) {
		Admininfo admin = adminInfo.selectByPrimaryKey(adminid);
		model.put("admin", admin==null?"":admin);
		return "/admin/admin_info";
	}
	
	@RequestMapping(value="/login",produces="application/json;charset=UTF-8")
	public @ResponseBody Admininfo login(String loginname,HttpServletRequest request,String loginpwd){
		System.out.println(loginname);
		System.out.println(MD5Util.getMd5(loginpwd));
		Admininfo admins=adminInfo.login(loginname, MD5Util.getMd5(loginpwd),request);
		request.getSession().setAttribute("admin", admins);
		return admins;
	}
	
	//根据条件查询
	@RequestMapping(value="/selectbyExample")
	public String findByWhere(@RequestParam(value="currPageNo",defaultValue="1") Integer currPageNo,
			@RequestParam(value="pageSize",defaultValue="10") Integer pageSize,String loginname,String regtime,ModelMap model){
		PageHelper.startPage(currPageNo, pageSize);
		List<Admininfo> list=adminInfo.findByWhere(loginname,regtime);
		PageInfo<Admininfo> pages=new PageInfo<Admininfo>(list,pageSize);
		model.put("name",loginname);
		model.put("time",regtime);
		model.put("pageInfo",pages);
		return "/admin/administrator";
	}
	
	@RequestMapping(value = "/doshow")
	public String doShow(String name, Integer currPageNo, Integer typeId, ModelMap model) {
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("selName", name);
		m.put("currPage", currPageNo);
		m.put("typeId", typeId);
		m = adminInfo.showPage(m);
		PageInfo<Admininfo> pages = (PageInfo<Admininfo>) m.get("pages");
		model.put("pageInfo", pages);
		return "admin/administrator";
	}
	
	//根据主键来修改信息
	@RequestMapping(value="/updateByPrimaryKey",produces="application/json;charset=UTF-8")
	public @ResponseBody boolean updateByPrimaryKey(HttpSession session,Admininfo admin,HttpServletRequest request){
		if(adminInfo.updateByPrimaryKeySelective(admin)){
			Admininfo admins = adminInfo.selectByPrimaryKey(admin.getAdminid());
			request.getSession().setAttribute("admin", admins);
			/*******写入日志******/
			Admininfo adminss= (Admininfo)session.getAttribute("admin");
			int id=100;
			if(adminss!=null){
				id=adminss.getAdminid();
			}
			AppendContentToFile.fileLogPrint(id+"", "修改了自己的信息");
			/*******END写入日志******/
			return true;
		}
		return false;
	}
	
	//根据主键来修改密码
	@RequestMapping(value="updatePwdByPrimaryKey",produces="application/json;charset=UTF-8")
	public @ResponseBody boolean updateByPrimaryKey(HttpSession session,Admininfo admin,String newPwd,HttpServletRequest request){
		Admininfo admins = adminInfo.selectByPrimaryKey(admin.getAdminid());
		if(admins.getLoginpwd().equals(MD5Util.getMd5(admin.getLoginpwd()))){
			admin.setLoginpwd(MD5Util.getMd5(newPwd));
			return adminInfo.updateByPrimaryKeySelective(admin);
		}
		/*******写入日志******/
		Admininfo adminss= (Admininfo)session.getAttribute("admin");
		int id=100;
		if(adminss!=null){
			id=adminss.getAdminid();
		}
		AppendContentToFile.fileLogPrint(id+"", "管理员修改了自己的密码");
		/*******END写入日志******/
		return false;
	}
	
	//根据主键修改状态
	@RequestMapping(value="/updateByPrimaryKeySelective",produces="application/json;charset=UTF-8")
	public @ResponseBody boolean updateByPrimaryKeySelective(HttpSession session,Integer userid,Integer userStaus) throws IOException{
		Admininfo admin=adminInfo.selectByPrimaryKey(userid);
		if(userStaus==0){//0冻结
			admin.setAdminstatus((byte)1);
		}else{
			admin.setAdminstatus((byte)0);
		}
		/*******写入日志******/
		Admininfo admins= (Admininfo)session.getAttribute("admin");
		int id=100;
		if(admins!=null){
			id=admins.getAdminid();
		}
		AppendContentToFile.fileLogPrint(id+"", "管理员对管理员"+admin.getAdminid()+"进行冻结和解冻");
		/*******END写入日志******/
		return adminInfo.updateByPrimaryKeySelective(admin);
	}
	
	//注销
	@RequestMapping(value="/exit")
	public String outLogin(HttpSession session){
		//通过session.invalidate()方法来注销当前的session
		if(session.getAttribute("admin")!=null) session.removeAttribute("admin");
		/*******写入日志******/
		Admininfo admins= (Admininfo)session.getAttribute("admin");
		int id=100;
		if(admins!=null){
			id=admins.getAdminid();
		}
		AppendContentToFile.fileLogPrint(id+"", "管理员进行了注销");
		/*******END写入日志******/
		return "redirect:/admin/login.jsp";
	}
}
